#ifndef __WEB_TOOL_API_H__
#define __WEB_TOOL_API_H__

#include <rtthread.h>
#include <at_socket.h>
#include "fal.h"

#define OCPP_SYS_CA_DIR        "/ca_lib/"
#define OCPP_MAIN_CA_PATH      "/ca_ocpp.txt"

#define OCPP_CA_NUM_MAX 24
#define OCPP_CA_FILE_NAME 64
#define OCPP_CA_FILE_PATH 128

#define WEB_FACTORY_UPLOAD_CGI_NAME "factory_web_upload"

typedef struct {
    char filename[OCPP_CA_FILE_NAME];
    int size;
} ocpp_ca;

typedef struct {
    ocpp_ca ocpp_ca_list[OCPP_CA_NUM_MAX];
    int ocpp_ca_count;
} ocpp_ca_info;

typedef struct {
    char login_id[50];
    rt_bool_t valid;
    int expire_time;
    struct rt_mutex lock;
} login_user_t;

enum web_user_status {
    WEB_USER_NOT_VERIFIED_OR_EXPIRED = 0,
    WEB_USER_AVAILABLE,
};

enum web_upload_events {
    WEB_UPLOAD_SYS_FW = 1,  /* 100 */
    WEB_UPLOAD_WEB_FW,      /* 101 */
    WEB_UPLOAD_15118_FW,    /* 102 */
    WEB_UPLOAD_15118_CER,   /* 103 */
    WEB_UPLOAD_OCPP_CER,    /* 104 */
    WEB_UPLOAD_REVERSED1,   /* 105 */
    WEB_UPLOAD_REVERSED2,   /* 106 */
    WEB_UPLOAD_REVERSED3,   /* 107 */
    
    WEB_UPLOAD_EVENT_MAX,
};

const static char web_upload_event_str[][32] = {
    "uploading system firmware",
    "uploading web firmware",
    "uploading 15118 firmware",
    "uploading 15118 certificate",
    "uploading OCPP certificate",
    "preserved",
    "preserved",
    "preserved",
};

const static enum web_upload_events support_events[WEB_UPLOAD_EVENT_MAX - 1] = {
    WEB_UPLOAD_SYS_FW,
    WEB_UPLOAD_WEB_FW,
    WEB_UPLOAD_15118_FW,
    WEB_UPLOAD_15118_CER,
    WEB_UPLOAD_OCPP_CER,
};

extern const char *_not_verified_or_expired;

/* check user and save id */
char *web_tool_get_user_id(void);
rt_bool_t web_tool_check_user_id(char *id);
rt_bool_t web_tool_verify_user(char *username, char *password, char *id);
    
/* check string */
int web_tool_str_end_with(const char* s, const char* t);
int web_tool_str_begin_with(const char *s, const char *t);

/* get param value */
int web_tool_get_param(char *key, char *value, int len);

/* param bind config */
void web_tool_sys_config(char *config_info, char *result, int size);

/* check upload filename */
char *web_tool_check_upload_filename(char *filename, int filesize, int event);

/* file operation */
int web_tool_delete_directory(char *pathname);
int web_tool_create_file(char *filepath);

/* decompress */
rt_bool_t web_tool_decompress_file(char *input);

/* ocpp */
rt_bool_t web_tool_set_ocpp_cer_by_name(char *name);
void web_tool_get_ocpp_cer_list(ocpp_ca_info *ocpp_ca_info);
rt_bool_t web_tool_get_file_summary(char *file_path, rt_uint8_t hash[16]);
void web_tool_get_current_ocpp_cer(char *ca_name, int name_len, ocpp_ca_info *ocpp_ca_i);

/* wifi info */
char *web_tool_wifi_ssid_get(void);
char *web_tool_wifi_pwd_get(void);

/* get web version */
char *web_tool_get_version(char **version);

/* check web firmware */
rt_bool_t web_tool_check_web_firmware(void);

/* Updates the system firmware with the specified firmware package */
int copy_to_dl_part(char *file_path, int file_size);

/* recv util get specified string */
int http_recv_str_end(int socket, char *buffer, int length, char *end_str, int end_length, rt_tick_t timeout);

#endif
